﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using Akabana.Amf.Util;

namespace Akabana.Amf.Messaging.Messages
{
    public class AbstractMessage
    {
        public Object body { get; set; }

        public String clientId { get; set; }

        public String destination { get; set; }

        public Dictionary<String, Object> headers { get; set; }

        public String messageId { get; set; }

        public Int64 timestamp { get; set; }

        public Int64 timeToLive { get; set; }

        public virtual AcknowledgeMessage CreateAcknowledgeMessage(object body)
        {
            AcknowledgeMessage result;
            
            var session = HttpContext.Current.Session;
            var messagingVersion = session["DSMessagingVersion"];
            if( messagingVersion == null )
            {
                if (this.headers.ContainsKey("DSMessagingVersion"))
                {
                    messagingVersion = this.headers["DSMessagingVersion"];
                    session["DSMessagingVersion"] = messagingVersion;
                }
            }
            var messagingVersionNum = messagingVersion == null ? 0 : Int32.Parse(messagingVersion.ToString());

            switch(messagingVersionNum)
            {            
                case 0:
                    result = new AcknowledgeMessage();
                    break;

                default:
                    result = new AcknowledgeMessageExt();
                    break;
            }
            
            result.body = body;
            result.messageId = Guid.NewGuid().ToString().ToUpper();
            result.timestamp = DateUtil.ToUtcMillSec(DateTime.Now);

            return result;
        }
    }
}
